
% A2a_price_index_entry
%==========================================================================

% Description: This code computes product-specific price indexes in the
% entry-only case, both factually and counterfactually 

clear
clc

load('main_data_supply.mat');

for i_k = 1:1011
   
i_k 

% Import Data and Estimates for product k
%==========================================================================

filename_costs = sprintf('cost_estimates/costs_%i_k.mat', i_k);  

if exist(filename_costs, 'file') == 2

k = index_price_coeff(i_k,1);    


data_k = data_summary{k};
numProdsTotal = size(data_k,1);
X_MPEC_k = X_MPEC_summary{k};
X_MPEC_opt = X_MPEC_opt_summary{k};
share_k = data_k(:,5);
price_k = data_k(:,7) + 1;
gdp_capita_k = data_k(:,13);
distance_k = data_k(:,14);
population_k = data_k(:,16);
population_log_k = log(population_k);
Y_k = Y_summary{k};
FE_k = FE_summary{k};
market_id_k = data_k(:,end);
T = market_id_k(end,1);
nn = size(Y_k,2);
alpha_constant = X_MPEC_opt(1,1);
alpha_distance = X_MPEC_opt(2,1);
alpha_population = X_MPEC_opt(3,1);
alpha_price = X_MPEC_opt(4,1);
alpha_1 = X_MPEC_opt(12,1);
alpha_FE = X_MPEC_opt((3*(K_A+1) + 1):(3*(K_A+1) + (K - K_A)), 1);

xi_k = X_MPEC_opt((3*(K_A+1) + (K - K_A) + 2 + 1):(3*(K_A+1) + (K - K_A) + 2 + numProdsTotal), 1) + 10;     % Ensure positive values
xi_k = xi_k - alpha_constant - log(price_k)*alpha_price - alpha_population*population_log_k - alpha_distance*distance_k;
q_k = alpha_constant + xi_k + FE_k*alpha_FE;


prodsMarket = zeros(T,1);
prodsMarket_temp = data_k(:,17);
for t = 1:T
prodsMarket_temp2 = prodsMarket_temp(market_id_k==t);
prodsMarket(t,1) = prodsMarket_temp2(1,1);
end

marketStarts = zeros(T,1);
marketEnds = zeros(T,1);
marketStarts(1) = 1;
marketEnds(1) = prodsMarket(1);
for t=2:T
    marketStarts(t) = marketEnds(t-1) + 1;
    marketEnds(t) = marketStarts(t) + prodsMarket(t) - 1;
end;

marketForProducts = zeros(numProdsTotal,1);
for t=1:T
marketForProducts(marketStarts(t):marketEnds(t)) = t;
end
numProdsTotal = size(data_k, 1);


delta_k =  alpha_constant + xi_k + log(price_k)*alpha_price + alpha_population*population_log_k + alpha_distance*distance_k;

for t = 1:T   
Y_market = Y_k(t,:);
expmu(marketStarts(t):marketEnds(t),:) = exp(log(price_k(marketStarts(t):marketEnds(t),:))*alpha_1*Y_market + repmat(FE_k(marketStarts(t):marketEnds(t),:)*alpha_FE,1,nn));
mu_true(marketStarts(t):marketEnds(t),:)= log(price_k(marketStarts(t):marketEnds(t),:))*alpha_1*Y_market + repmat(FE_k(marketStarts(t):marketEnds(t),:)*alpha_FE,1,nn);
end
expmeanval = exp(delta_k);
meanval_true = delta_k;
oo = ones(1,nn);                  
sharesum = sparse(zeros(T,numProdsTotal));  % used to create denominators in logit predicted shares (i.e. sums numerators)
for t = 1:T
    sharesum(t,marketStarts(t):marketEnds(t)) = 1;
end
numer = (expmeanval*oo ).*expmu;  
v_true = (meanval_true*oo ) + mu_true; 
sum1 = sharesum*numer;
sum11 = 1./(0+sum1);                                                       % Will not result in share_k = EstShare_true
denom1 = sum11(marketForProducts,:);    
simShare_true = numer.*denom1;              
EstShare_true = mean(simShare_true,2);  

%==========================================================================


% Compute Utility and Price Index
%==========================================================================

% Factual
%--------------------------------------------------------------------------

filename_costs = sprintf('cost_estimates/costs_%i_k.mat', i_k);  
load(filename_costs, 'mc_opt', 'f_vec', 'mc_opt_summary')
N_true = (price_k - mc_opt) ./ price_k .* EstShare_true ./ f_vec;
N_counter = (price_k - mc_opt) ./ price_k  ./ f_vec;
numer_counter = numer .* repmat(N_counter, 1, nn) ./ repmat(N_true, 1, nn);
v_counter = log(numer_counter); 


P_true = zeros(T,nn);
beta_price = Y_k * alpha_1 + alpha_price;
for tt = 1:T
v_true_t = v_true(marketStarts(tt,1):marketEnds(tt,1),:,:); 
beta_price_t = beta_price(tt, :);
exp_v_true_t = exp(v_true_t);
s_exp_v_true_t = sum(exp_v_true_t);
ln_s_exp_v_true_t = log(s_exp_v_true_t);
P_true(tt,:) = exp(1) * exp(1./beta_price_t .* ln_s_exp_v_true_t) .* gamma(1 - 1./beta_price_t);
end

% Counterfactual
%--------------------------------------------------------------------------

P_counter = zeros(T,nn);
beta_price = Y_k * alpha_1 + alpha_price;
for tt = 1:T
data_k_t = data_k(marketStarts(tt,1):marketEnds(tt,1),:);   
index_domestic = data_k_t(:,3) == data_k_t(:,4);
v_counter_t = v_counter(marketStarts(tt,1):marketEnds(tt,1),:,:); 
v_counter_t = v_counter_t(index_domestic, :);
beta_price_t = beta_price(tt, :);
exp_v_counter_t = exp(v_counter_t);
s_exp_v_counter_t = exp_v_counter_t;
ln_s_exp_v_counter_t = log(s_exp_v_counter_t);
if sum(index_domestic) == 1
P_counter(tt,:) = exp(1) * exp(1./beta_price_t .* ln_s_exp_v_counter_t) .* gamma(1 - 1./beta_price_t);
end
if sum(index_domestic) == 0
P_counter(tt,:) = exp(1) * ones(1, nn);
end
end

% Sort results
%--------------------------------------------------------------------------

beta_price = Y_k * alpha_1 + alpha_price;
ratio = P_counter ./ P_true;

ratio_sorted = zeros(T, nn);
P_true_sorted = zeros(T, nn);
P_counter_sorted = zeros(T, nn);
beta_price_sorted = zeros(T, nn);
u_true_avg_sorted = zeros(T, nn);
u_counter_avg_sorted = zeros(T, nn);
for tt = 1:T
[Y_k_tt, ind] = sort(Y_k(tt,:));
P_true_sorted(tt,:) = P_true(tt,ind);
P_counter_sorted(tt,:) = P_counter(tt,ind);
ratio_sorted(tt,:) = ratio(tt,ind);
beta_price_sorted(tt,:) = beta_price(tt,ind);
end


diff = ratio_sorted(:,20) - ratio_sorted(:,80);
P_true_sorted_20 = P_true_sorted(:,20);
P_true_sorted_80 = P_true_sorted(:,80);
P_counter_sorted_20 = P_counter_sorted(:,20);
P_counter_sorted_80 = P_counter_sorted(:,80);
beta_price_20 = beta_price_sorted(:,20);
beta_price_80 = beta_price_sorted(:,80);

%==========================================================================


% Save Output to file
%==========================================================================

year_t = zeros(T,1);
quarter_t = zeros(T,1);
declarant_t = zeros(T,1);
price_domestic = zeros(T,1);
gdp_capita_domestic = zeros(T,1);
domestic_producer = zeros(T,1);
for tt = 1:T
data_tt = data_k(marketStarts(tt,1):marketEnds(tt,1),:);     
year_t(tt,1) = data_tt(1,1);
quarter_t(tt,1) = data_tt(1,2);
declarant_t(tt,1) = data_tt(1,3);
index_domestic = data_tt(:,3) == data_tt(:,4);
if sum(index_domestic) == 1
price_domestic(tt,1) = data_tt(index_domestic,7);
gdp_capita_domestic(tt,1) = data_tt(index_domestic,13);
domestic_producer(tt,1) = 1;
end
end


filename_table = sprintf('price_index/product_specific/diff_entry_table_%i_k_test.csv', i_k); 
data_save = array2table([data_k(:,1:4), price_k, P_true_sorted_20(marketForProducts,:), P_true_sorted_80(marketForProducts,:), P_counter_sorted_20(marketForProducts,:), P_counter_sorted_80(marketForProducts,:), beta_price_20(marketForProducts,:), beta_price_80(marketForProducts,:), mc_opt_summary(:,3)]);
data_save.Properties.VariableNames = {'Year' 'Quarter' 'Declarant' 'Partner', 'price', 'P_true_sorted_20', 'P_true_sorted_80', 'P_counter_sorted_20', 'P_counter_sorted_80', 'beta_price_20', 'beta_price_80', 'mc_flag'};  
writetable(data_save, filename_table, 'Delimiter',',','QuoteStrings',true);

filename_diff_csv = sprintf('price_index/product_specific/diff_entry_%i_k_test.csv', i_k);  
csvwrite(filename_diff_csv,[year_t, quarter_t, declarant_t, price_domestic, gdp_capita_domestic, domestic_producer, P_true_sorted_20, P_true_sorted_80, P_counter_sorted_20, P_counter_sorted_80, beta_price_20, beta_price_80]);
filename_diff_matlab = sprintf('price_index/product_specific/diff_entry_%i_k_test.mat', i_k);  
save(filename_diff_matlab, 'year_t', 'quarter_t', 'declarant_t', 'price_domestic', 'gdp_capita_domestic', 'data_k', 'domestic_producer', 'P_true_sorted_20', 'P_true_sorted_80', 'P_counter_sorted_20', 'P_counter_sorted_80', 'beta_price_20', 'beta_price_80');  

clearvars -except Y_summary index_price_coeff data_summary FE_summary i_k income_mu_sigma  K K_A KK objective_MPEC_summary price_coeff_MPEC price_table status_MPEC_index status_MPEC_summary v_summary X_MPEC_opt_summary X_MPEC_summary

end

end
